package com.example.bill.sys;

import android.util.Log;
import android.view.Choreographer;

public class ChoreographerHelper implements Choreographer.FrameCallback {

    private final Choreographer choreographer;
    private long lastFrameTimeNanos;

    public static final String ChoreographerHelper_tag = "ChoreographerHelper";

    public ChoreographerHelper() {

        choreographer = Choreographer.getInstance();
    }

    public void run() {

        choreographer.postFrameCallback(this);
    }

    @Override
    public void doFrame(long frameTimeNanos) {

        if (lastFrameTimeNanos != 0) {

            long duration = (frameTimeNanos - lastFrameTimeNanos) / 1_000_000;
            if (duration > 16) {


                float frameCount = duration / 16.6f;
                Log.w(ChoreographerHelper_tag, "执行时长" + duration + "ms" + " 掉帧=" + frameCount);

            }
        }

        lastFrameTimeNanos = frameTimeNanos;
        choreographer.postFrameCallback(this);
    }
}
